SPB Git

Repository Public MIT

route.ts 1.1 KB 20 lines typescript

1import { join } from "node:path";2import { getCourse } from "@/lib/content";3import { env } from "@/lib/env";4import { notFoundResponse, serveObject } from "@/lib/serve";5import { FsStorage } from "@/modules/storage";67export const dynamic = "force-dynamic";89/**10 * Resource downloads. A file is served only when the resource is published, the instructor enabled its11 * download, a licence is recorded and any delayed release date has passed (see `canDownload`). The address12 * names the resource, not the file: replacing the file in the studio does not break links.13 */14export async function GET(request: Request, { params }: { params: Promise<{ session: string; resource: string }> }) {15  const { session: sessionId, resource: resourceId } = await params;16  const resource = (await getCourse()).sessions.find((s) => s.id === sessionId)?.resources.find((r) => r.id === resourceId);17  if (!resource?.downloadable) return notFoundResponse();18  return serveObject(new FsStorage(join(env.dataDir, "resources")), resource.path, request, { cache: "public, max-age=300", download: resource.path.split("/").pop()!, type: resource.mime });19}20
Files main
Menu

Theme